package org.demo.tools;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.base.Splitter;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import com.google.common.collect.Multiset;
import com.google.common.collect.Ordering;
import com.google.common.collect.SetMultimap;
import com.google.common.collect.Sets;
import com.google.common.collect.Sets.SetView;
import com.google.common.collect.TreeMultimap;
import com.google.common.primitives.Ints;

public class CollectionsTest {

	
	public static void main(String[] args) {
		Map<String, Map<String, String>> map = Maps.newHashMap();
		Map<String, Object> cs1 = Maps.newHashMap();
	    cs1.put("SkuId", 1);  
	    cs1.put("Color", 1); 
	    cs1.put("Size", 1); 
		
//		 System.out.println(Ints.MAX_POWER_OF_TWO);
//		 //向左移位
//		 System.out.println(1 << (Integer.SIZE - 2));
		 
		 
		
		System.out.println("ImmutableMap="+ImmutableMap.of("ON","TRUE","OFF","FALSE"));
		
		
		
		List<String> list = Lists.newArrayList("one","two","three");
		
		System.out.println(list);
		
//		Lists.newArrayList(  
//	            (List<?>)Lists.newArrayList("热点", Lists.newArrayList("超薄7mm以下", "支持NFC")),  
//	            (List<?>)Lists.newArrayList("系统", "苹果（IOS）"),  
//	            (List<?>)Lists.newArrayList("系统", "苹果（IOS）"),  
//	            (List<?>)Lists.newArrayList("购买方式", "非合约机")  
//	    );  
		
		//不可变集合
		ImmutableList<String> of = ImmutableList.of("one","two","three");
		System.out.println(of);
		//jdk 也提供了
		Collections.unmodifiableList(list);
		
		ImmutableList<List<String>> builder = ImmutableList.<List<String>>builder().add(Lists.newArrayList("系统", "苹果（IOS）")).build();
		
//		深拷贝
		ImmutableList<String> copyOf = ImmutableList.copyOf(list);
//		相当于有两个key的map
		HashBasedTable<Integer, Integer, String> create = HashBasedTable.create();
		create.put(1, 1, "1");
		
		
		Multiset<Integer> multiSet = HashMultiset.create();
		
		Set<String> wordsWithPrimeLength = ImmutableSet.of("one", "two", "three", "six", "seven", "eight");
		Set<String> primes = ImmutableSet.of("two", "three", "five", "seven");
		SetView<String> intersection = Sets.intersection(primes,wordsWithPrimeLength);
		// intersection包含"two", "three", "seven"
		intersection.immutableCopy();//可以使用交集，但不可变拷贝的读取效率更高
		
//		Sets.powerSet(null);
		
//		有一组对象，它们在某个属性上分别有独一无二的值，而我们希望能够按照这个属性值查找对象—
//		比方说，我们有一堆字符串，这些字符串的长度都是独一无二的，而我们希望能够按照特定长度查找字符串：
		
		
		Iterable<String> iterable = Splitter.on(",").trimResults().omitEmptyStrings().split("w3,w2,w1");
		
		
		Map<String, String> split = Splitter.on(",").withKeyValueSeparator("=").split("w3=10,w2=10,w1=80");
		
		System.out.println("split----Map<String, String>="+split);
		
		list =	Lists.<String>newArrayList(iterable); 
	
		 Comparator<String> userComparator = Ordering.from(new Comparator<String>() {

			@Override
			public int compare(String o1, String o2) {
				
				return o1.compareTo(o2);
			}
		});
		 
		 Collections.sort(list, userComparator);
		 
		 System.out.println("=========="+list);
		
	
		
		ImmutableMap<Integer, String> stringsByIndex = Maps.uniqueIndex(Lists.<String>newArrayList("a","123","fewf"),
				new Function<String, Integer>() {
					public Integer apply(String string) {
//						要求长度唯一
						return string.length();
					}
				});
		System.out.println("stringsByIndex="+stringsByIndex.toString());
		
//		类似分组的功能
		ImmutableSet digits = ImmutableSet.of("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine");
			Function<String, Integer> lengthFunction = new Function<String, Integer>() {
			    public Integer apply(String string) {
			        return string.length();
			    }
			};
			ImmutableListMultimap<Integer, String> digitsByLength= Multimaps.index(digits, lengthFunction);
			
//			System.out.println("inverse="+digitsByLength.inverse());
			
			System.out.println("digitsByLength="+digitsByLength.toString());
			
			
//			map 里放 list
			ArrayListMultimap<String, Integer> multimap = ArrayListMultimap.create();
			multimap.putAll("b", Ints.asList(2, 4, 6));
			multimap.putAll("a", Ints.asList(4, 2, 1));
			multimap.putAll("c", Ints.asList(2, 5, 3));
			multimap.put("d", 6);
			
			TreeMultimap<Integer, String> inverse = Multimaps.invertFrom(multimap, TreeMultimap.<Integer, String>create());
			//注意我们选择的实现，因为选了TreeMultimap，得到的反转结果是有序的
			/*
			* inverse maps:
			*  1 => {"a"}
			*  2 => {"a", "b", "c"}
			*  3 => {"c"}
			*  4 => {"a", "b"}
			*  5 => {"c"}
			*  6 => {"b"}
			*/	
			System.out.println("multimap="+multimap.toString());
//			multimap={d=[6], b=[2, 4, 6], c=[2, 5, 3], a=[4, 2, 1]}
			
			System.out.println("inverse="+inverse.toString());
//			inverse={1=[a], 2=[a, b, c], 3=[c], 4=[a, b], 5=[c], 6=[b, d]}
			
			
				Map<String, Integer> map1 = ImmutableMap.of("a", 1, "b", 1, "c", 2);

//			SetMultimap 只读的
				SetMultimap<String, Integer> forMap = Multimaps.forMap(map1);
				System.out.println("map1="+map1);
//					map1={a=1, b=1, c=2}
				System.out.println("forMap="+forMap);
			
//				forMap={a=[1], b=[1], c=[2]}
				Multimap<Integer, String> inverse1 = Multimaps.invertFrom(forMap, HashMultimap.<Integer, String>create());
				System.out.println("inverse1="+inverse1);
				
				System.out.println("---equal="+Objects.equal(new Integer("1"),"1"));
				
				System.out.println(new Integer("1").equals(new Integer("1")));
				
	}
	
}
